﻿using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace TestDatatable
{
    public partial class PJsCodeEdit: UserControl
    {
        private string m_JsCodePathName = "";

        public PJsCodeEdit()
        {
            InitializeComponent();

            panel1.Paint += Panel_Paint; // 绑定 Paint 事件

        }

        void Panel_Paint(object sender, PaintEventArgs e)
        {
            Control p = sender as Control;
            using (Pen pen = new Pen(m_PrimaryColor, 5))
            {
                e.Graphics.DrawRectangle(pen, 0, 0, p.Width, p.Height);
            }

  
        }


        public string getJsCode()
        {
            return richTextBoxCode.Text;
        }

        private Color m_PrimaryColor = Color.Red;


        public void setPrimaryColor(Color color)
        {
            m_PrimaryColor = color;
        }

        public void setSecondaryColor(Color color)
        {
            m_SecondaryColor = color;
        }
 

        private Color m_SecondaryColor = Color.Red;
    

        private string m_JsCode = "";


        public bool ShowJsCode(string jsCodePath) {

          
            richTextBoxCode.Text = jsCodePath;
            richTextBoxCode.Refresh();
    

            return true;
        }

  
        /// <summary>
        /// 递归遍历指定控件及其所有子控件
        /// </summary>
        /// <param name="parent">父控件</param>
        private void TraverseControls(Control parent)
        {
            foreach (Control ctrl in parent.Controls)
            {
                // 例如：输出控件的名称和类型
                // Console.WriteLine($"控件名称: {ctrl.Name}, 类型: {ctrl.GetType().Name}");

                if (ctrl.GetType() == typeof(Button))
                {
                    Button btn = (Button)ctrl;
                    btn.BackColor = m_PrimaryColor;
                    btn.ForeColor = Color.White;
                    btn.FlatAppearance.BorderColor = m_SecondaryColor;
                }

                if (ctrl.GetType() == typeof(Label))
                {
                    Label label = (Label)ctrl;
                    label.ForeColor = m_SecondaryColor; 
                }

                if (ctrl.GetType() == typeof(RichTextBox))
                {
                    RichTextBox rtb = (RichTextBox)ctrl;
                    rtb.ForeColor = m_SecondaryColor;
                }


                // 如果当前控件包含子控件，递归调用
                if (ctrl.Controls.Count > 0)
                {
                    TraverseControls(ctrl);
                }
            }


        }

        public void LoadTheme()
        {
            TraverseControls(this);

            this.panel1.Invalidate(); // 强制触发重绘
        }




        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(m_JsCodePathName))
            {
                SaveAsFile(sender, e);
            }
            else
            {
                File.WriteAllText(m_JsCodePathName, richTextBoxCode.Text);
                MessageBox.Show("文件保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void SaveAsFile(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                m_JsCodePathName = dlg.FileName;
                File.WriteAllText(m_JsCodePathName, richTextBoxCode.Text);

            }
        }

        private void PJsCodeEdit_Load(object sender, EventArgs e)
        {

        }

        private void buttonSaveTo_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
